<?php
require_once APP_PATH.'models'.DS.'jadwal_kerja'.DS.'JadwalKerjaBL.php';
require_once APP_PATH.'models'.DS.'jam_kerja'.DS.'JamKerjaBL.php';
require_once APP_PATH.'models'.DS.'divisi'.DS.'DivisiBL.php';
require_once APP_PATH.'models'.DS.'karyawan'.DS.'KaryawanBL.php';
require_once APP_PATH.'models'.DS.'karyawan_jadwal_kerja'.DS.'KaryawanJadwalKerjaBL.php';

class AbsensiController extends AppController {
   
   public $name = 'Absensi';
   public $uses = array();
   public $components = array('Session');
   
   public function index() {
      return true;
   }
   
   public function jamKerjaPembuatanPage() {
      if (!empty($this->data)) {
         $jamKerjaBL = new JamKerjaBL();
         
         if ($jamKerjaBL->createJamKerja($this->data)) {
            $this->Session->setFlash(__('Jam kerja baru telah berhasil dibuat',true));
            $this->redirect(array('action' => 'jamKerjaPembuatanPage'));
         } else {
            $this->Session->setFlash(__('Jam kerja Gagal dibuat',true));
         }
      }
   }
   
   public function jamKerjaEditPage() {
   }
   
   public function jadwalKerjaPembuatanPage() {
      
      // --- Process data
      if (!empty ($this->data)) {      
         
         // --- create cakephp array structure for save() method
         if ($this->Session->check('JadwalKerja')) {
            $jadwalKerjaBL = new JadwalKerjaBL();
            
            $save = $jadwalKerjaBL->createJadwalKerja($this->data, $this->Session->read('JadwalKerja'));
            if (!is_array($save)) {
               $this->Session->setFlash(__('Jadwal baru telah berhasil dibuat', true));
               $this->redirect(array('action' => 'jadwalKerjaPembuatanPage'));
            } else {
               $this->Session->setFlash(__('Gagal membuat jadwal. Silakan ulangi', true));
            }

            if ($this->Session->check('JadwalKerja')) {
               $this->Session->delete('JadwalKerja');
            }
         }
      }
      
      if ($this->Session->check('JadwalKerja')) {
         $this->Session->delete('JadwalKerja');
      }
      
      $defaultDays = array(
          0 => 'Hari 1',
          1 => 'Hari 2',
          2 => 'Hari 3',
          3 => 'Hari 4',
          4 => 'Hari 5',
          5 => 'Hari 6',
          6 => 'Hari 7',
          7 => 'Hari 8'
      );
      
      $cycles = array();
      
      for ($i=1; $i<=31; $i++) {
         $cycles[$i] = $i;
      }
      
      $jamKerjaBL = new JamKerjaBL();
      
      $this->set('jam_kerja_list',$jamKerjaBL->jamKerjaFind('list'));
      $this->set('cycles',$cycles);
      $this->set('defaultDays',$defaultDays);
      
      return true;
      
   }
   
   public function karyawanJadwalKerjaPembuatanPage() {
      $divisiBL = new DivisiBL();
      $jadwalKerjaBL = new JadwalKerjaBL();
      
      if (!empty($this->data)) {
         
         // --- save the data
         $dataFix = $this->karyawanJadwalKerjaStandardStructure($this->data);
         $karyawanJadwalKerjaBL = new KaryawanJadwalKerjaBL();
         
         if ($karyawanJadwalKerjaBL->createKaryawanJadwalKerja($dataFix)) {
            $this->Session->setFlash(__('Penjadwalan Karyawan Berhasil Dilakukan. Data Telah Disimpan', true));
            $this->redirect(array('action' => 'karyawanJadwalKerjaPembuatanPage'));
            
         } else {
            $this->Session->setFlash(__('Error: Penjadwalan Gagal. Data Tidak Tersimpan', true));
         }  
         
         // --- if data saving process is failed, then sets some vars to the view to fill form default vals
         $this->set('submitted','yes');
         $this->set('assign_mode', $this->data['Foo']['assign_mode']);
         
         if ($this->data['Foo']['assign_mode'] == 'some') {
            $karyawanBL = new KaryawanBL();

            $options = array(
                'conditions' => array('Karyawan.divisi_id' => $this->data['Foo']['divisi_id'])
            );

            $this->set('karyawan_list', $karyawanBL->karyawanFind('list',$options));
         } else {
            $this->set('karyawan_list', array('Satu','Dua','Tiga'));
         }
         
      } else {
         // --- sets the default vals for default state before form to be submitted
         $this->set('submitted','no');
         $this->set('assign_mode', 'null');
         $this->set('karyawan_list', array());
      }
      
      // --- sets the default vals for default state before form to be submitted
      $this->set('jadwal_kerja_list',$jadwalKerjaBL->jadwalKerjaFind('list'));
      $this->set('divisi_list',$divisiBL->divisiFind('list'));

      $this->set('radio_choice',array(
          'all' => 'Set jadwal untuk SEMUA karyawan di sebuah divisi', 
          'some' => 'Set jadwal untuk BEBERAPA karyawan di sebuah divisi'
         )
      );
     
   }
   
   public function indexKaryawanJadwalKerja() {
      $this->set('karyawan_jadwal_kerja_list', $this->paginate(ClassRegistry::init('KaryawanJadwalKerja')));
      ClassRegistry::flush();
   }


   private function karyawanJadwalKerjaStandardStructure(array $data=array()) {
      $dataFixStructure = array();
      
      if ($data['Foo']['assign_mode'] == 'all') {
         $karyawanBL = new KaryawanBL();         
         $karyawan_list = $karyawanBL->karyawanFind('all', array('conditions' => array('Karyawan.divisi_id' => $data['Foo']['divisi_id'])));
         
         foreach ($karyawan_list as $karyawan) {
            array_push($dataFixStructure, array(
                  'KaryawanJadwalKerja' => array(
                      'karyawan_id' => $karyawan['Karyawan']['id'],
                      'jadwal_kerja_id' => $data['Foo']['jadwal_kerja_id']
                  )
               )
            );
         }
         
      } else if ($data['Foo']['assign_mode'] == 'some') {
         foreach ($data['Foo']['karyawan_id'] as $id) {
            array_push($dataFixStructure, array(
                  'KaryawanJadwalKerja' => array(
                      'karyawan_id' => $id,
                      'jadwal_kerja_id' => $data['Foo']['jadwal_kerja_id']
                  )
               )
            );
         }
      }
      
      return $dataFixStructure;
      
   }
}
